<!--
 * 超链接
 * @作者: 陈老师儿~
 * @手机: 18560000860
 -->
<template>
    <view class="pure-link" :class="classes" :style="styles" :hover-class="hoverClass" @tap="handleClick">
        <slot v-if="$slots.default"></slot>
        <template v-else>
            <!-- 前置图标 -->
            <view class="pure-link-icon pure-link-icon-prefix" v-if="prefixIcon">
                <pure-icon :name="prefixIcon" :mode="prefixIconMode"></pure-icon>
            </view>

            <!-- 文本 -->
            <text class="pure-link-text" v-if="text">{{ text }}</text>

            <!-- 后置图标 -->
            <view class="pure-link-icon pure-link-icon-suffix" v-if="suffixIcon">
                <pure-icon :name="suffixIcon" :mode="suffixIconMode"></pure-icon>
            </view>
        </template>
    </view>
</template>

<script>
    import props from "./props.js";
    export default {
        options: {
            // 将自定义节点设置成虚拟的
            // 头条、飞书：因为合并后丢失事件和 'provide/inject'
            // 京东：因为合并后不能合并外层 'class'
            // 百度：因为合并后会在控制台疯狂报警告
            // #ifndef MP-TOUTIAO || MP-LARK || MP-JD || MP-BAIDU
            virtualHost: true,
            // #endif

            // 组件样式隔离
            styleIsolation: "apply-shared",

            // 允许使用多个插槽
            multipleSlots: true,
        },
        props: props,
        emits: ["onClick"],
        computed: {
            // classes
            classes() {
                const classes = [];

                // 下划线
                if (this.underline) classes.push("pure-link-underline");

                // 禁用
                if (this.disabled) classes.push(this.disabledClass);

                return classes;
            },

            // styles
            styles() {
                const styles = [];
                return styles;
            },
        },
        methods: {
            // 点击事件
            handleClick() {
                // 禁用
                if (this.disabled) return;

                // 触发事件
                this.$emit("onClick");

                // 优先判断 APP 内打开外部链接
                // #ifdef APP-PLUS
                if (this.href) {
                    plus.runtime.openURL(this.href);
                    return;
                }
                // #endif

                // 打开内部链接
                // 不要用 uni[this.openType] 进行跳转，会有问题
                // navigateTo
                if (this.openType === "navigateTo" || this.openType === "to" || this.openType === "navigate") {
                    uni.navigateTo({
                        url: this.url,
                        animationType: this.animationType || "pop-in",
                        animationDuration: this.animationDuration,
                        events: this.events,
                        success: this.success,
                        fail: this.fail,
                        complete: this.complete,
                    });
                    return;
                }

                // redirectTo
                if (this.openType === "redirectTo" || this.openType === "redirect") {
                    uni.redirectTo({
                        url: this.url,
                        success: this.success,
                        fail: this.fail,
                        complete: this.complete,
                    });
                    return;
                }

                // reLaunch
                if (this.openType === "reLaunch" || this.openType === "relaunch") {
                    uni.reLaunch({
                        url: this.url,
                        success: this.success,
                        fail: this.fail,
                        complete: this.complete,
                    });
                    return;
                }

                // switchTab
                if (this.openType === "switchTab" || this.openType === "tab" || this.openType === "switch") {
                    uni.switchTab({
                        url: this.url,
                        success: this.success,
                        fail: this.fail,
                        complete: this.complete,
                    });
                    return;
                }

                // navigateBack
                if (this.openType === "navigateBack" || this.openType === "back") {
                    uni.navigateBack({
                        delta: this.delta,
                        animationType: this.animationType || "pop-out",
                        animationDuration: this.animationDuration,
                        success: this.success,
                        fail: this.fail,
                        complete: this.complete,
                    });
                    return;
                }
            },
        },
    };
</script>

<style scoped>
    @import "./style.css";
</style>
